package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author 林晓伟
 */
@RestController
@RequestMapping("/dish")
public class FoodController {
    @Resource
    private FoodService foodService;

    /**
     * H13-菜品新增
     */
    @PostMapping
    public R add(@RequestBody Food food) {
        Integer add = foodService.add(food);
        if (add != 0){
            return R.success("新增菜品成功");
        }
        return R.error("新增菜品失败");
    }

    /**
     * H12-菜品分页查询
     * @param name 名字
     * @param page 页数
     * @param pageSize 每页条数
     */
    @GetMapping("/page")
    public R s(@RequestParam(value="name",required=false)String name,
               @RequestParam Integer page,
               @RequestParam Integer pageSize){
        return foodService.select(page,pageSize,name);
    }

    /**
     * H12-菜品下拉列表
     */
    @GetMapping("/list")
    public R list(@RequestParam(value = "categoryId",required = false)String categoryId){
        return R.success(foodService.list(categoryId));
    }

    /**
     * H12-根据ID查询菜品
     */
    @GetMapping("/{id}")
    public R foodAll(@PathVariable Integer id){
        Food food = foodService.foodAll(id);
        return R.success(food);
    }

    /**
     * H14-菜品修改
     */
    @PutMapping
    public R update(@RequestBody Food food){
        Integer update = foodService.update(food);
        if (update != 0){
            return R.success("修改菜品成功");
        }
        return R.error("修改菜品失败");
    }

    /**
     * H15-批量删除菜品
     */
    @DeleteMapping
    public R delete(@RequestParam String ids){
        Integer delete = foodService.delete(ids);
        if (delete != 0){
            return R.success("删除成功！");
        }
        return R.error("删除失败");
    }

    /**
     * F16-菜品停售/起售
     */
    @PostMapping("/status/{status}")
    public R up(@PathVariable Integer status,@RequestParam String ids){
        Integer integer = foodService.statusUpdate(ids);
        if (integer != 0){
            return R.success("操作成功！");
        }
        return R.error("操作失败！");
    }


}